package com.moyu.admin.shiro;

import com.moyu.data.domain.SysUserLogin;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

public class PasswordHelper {

  private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
  private String algorithnName = "md5";
  private final int hashIterations = 2;

  public void encryptPassword(SysUserLogin user) {
    user.setSalt(randomNumberGenerator.nextBytes().toHex());
    String newPwd =
        new SimpleHash(
                algorithnName,
                user.getPassword(),
                ByteSource.Util.bytes(user.getUsername() + user.getSalt()),
                hashIterations)
            .toHex();
    user.setPassword(newPwd);
  }

  public static void main(String[] args) {
    SysUserLogin user = new SysUserLogin();
    user.setUsername("moyu");
    user.setPassword("123456");
    new PasswordHelper().encryptPassword(user);
    System.out.println(user);
  }
}
